package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.MainEquipmentSpinnerAdapter;
import com.glory.hikvisionmobile.adapter.PromptFankAdapter;
import com.glory.hikvisionmobile.adapter.TrackInItemWorkAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.TrackIn.GetCarrierBean;
import com.glory.hikvisionmobile.bean.TrackIn.ItemWorkBean;
import com.glory.hikvisionmobile.bean.TrackIn.TrackInLoadBean;
import com.glory.hikvisionmobile.bean.TrackIn.XPdaCarrier;
import com.glory.hikvisionmobile.bean.TrackIn.XPdaEquipment;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.customView.HikLabelEditText;
import com.glory.hikvisionmobile.customView.HikLabelSpinner;
import com.glory.hikvisionmobile.customView.HikLabelTextView;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class Work_Al5TeosEtchTrackInActivity extends BaseActivity {

    @BindView(R.id.lbLot)
    HikLabelTextView lbLot;
    @BindView(R.id.lbSpinner)
    HikLabelSpinner lbSpinner;
    @BindView(R.id.lbLotNumber)
    HikLabelTextView lbLotNumber;
    @BindView(R.id.lbCurrentStep)
    HikLabelTextView lbCurrentStep;
    @BindView(R.id.lbRecipe)
    HikLabelTextView lbRecipe;
    @BindView(R.id.lbRecipeDesc)
    HikLabelTextView lbRecipeDesc;
    @BindView(R.id.lbRQTime)
    HikLabelTextView lbRQTime;
    @BindView(R.id.rvWorkHintList)
    RecyclerView rvWorkHintList;
    @BindView(R.id.lbPieceCode)
    HikLabelTextView lbPieceCode;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvItemWorkList)
    RecyclerView rvItemWorkList;
    @BindView(R.id.llItemWorkLayout)
    LinearLayout llItemWorkLayout;

    private String lotNum;
    private PromptFankAdapter promptFankAdapter;
    private TrackInItemWorkAdapter childEquipmentAdapter;
    private String mainQty;
    private MainEquipmentSpinnerAdapter mainEquipmentSpinnerAdapter;
    private boolean isRequireEqp;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_al5teos_etch_track_in;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        lotNum = bundle.getString("lotNum");
        isRequireEqp = bundle.getBoolean("isRequireEqp", false);
        lbLot.getLabelContext().setText(lotNum);

        rvWorkHintList.setLayoutManager(new LinearLayoutManager(Work_Al5TeosEtchTrackInActivity.this));
        rvWorkHintList.setItemAnimator(new DefaultItemAnimator());
        rvWorkHintList.addItemDecoration(new DividerItemDecoration(Work_Al5TeosEtchTrackInActivity.this, 1));
        promptFankAdapter = new PromptFankAdapter(null);
        promptFankAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        promptFankAdapter.isFirstOnly(true);
        rvWorkHintList.setAdapter(promptFankAdapter);

        rvItemWorkList.setLayoutManager(new GridLayoutManager(this,2));
        rvItemWorkList.setItemAnimator(new DefaultItemAnimator());
        childEquipmentAdapter = new TrackInItemWorkAdapter(null);
        childEquipmentAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        childEquipmentAdapter.isFirstOnly(true);
        rvItemWorkList.setAdapter(childEquipmentAdapter);

        lbSpinner.getLabelContext().setDropDownVerticalOffset(getResources().getDimensionPixelSize(R.dimen.free_ui_dp_48));
        mainEquipmentSpinnerAdapter = new MainEquipmentSpinnerAdapter(null);
        lbSpinner.getLabelContext().setAdapter(mainEquipmentSpinnerAdapter);
        lbSpinner.getLabelContext().setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                XPdaEquipment equipment = mainEquipmentSpinnerAdapter.getData().get(position);
                searchItemDevices(equipment.getEquipmentId());
                lbSpinner.getLabelContext().setTag(equipment.getEquipmentId());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    protected void initData() {
        JsonObject map = new JsonObject();
        map.addProperty("actionType", "Check");
        map.addProperty("lotId", lotNum);

        NetUtils.requestDara("HIK.FankePdaTrackInLoad", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<TrackInLoadBean>>(
                new TypeToken<BaseResponseBean<TrackInLoadBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<TrackInLoadBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    TrackInLoadBean body = response.body().getBody();
                    lbCurrentStep.getLabelContext().setText(body.getCurrentStepNameDesc());
                    mainQty = body.getMainQty();
                    lbLotNumber.getLabelContext().setText(mainQty);
                    lbRQTime.getLabelContext().setText(body.getRQTime());
                    lbRecipe.getLabelContext().setText(body.getRecipeName());
                    lbPieceCode.getLabelContext().setText(body.getComponentIds());
                    lbRecipeDesc.getLabelContext().setText(body.getRecipeDesc());
                    promptFankAdapter.setNewData(body.getFankBeans());
                    mainEquipmentSpinnerAdapter.setData(body.getXPdaEquipment());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<TrackInLoadBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.FankePdaTrackInLoad");

            }
        });
    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("反刻待进站");
        freeUI_titleView.setRightListener("刷新", view -> {
            initPermission("Refresh", this, new ActionControlListener() {
                @Override
                public void onControlChangeListener() {
                    initData();
                }
            });

        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.tvReplay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvReplay:
                initPermission("TrackIn", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        if (isRequireEqp){
                            if (((String)lbSpinner.getLabelContext().getTag()).length() == 0) {
                                showToast("主设备不能为空", false);
                                return;
                            }
                            if (llItemWorkLayout.getVisibility() == View.VISIBLE){
                                if (FreeApi_StringUtils.isEmpty(childEquipmentAdapter.getItemWork())) {
                                    showToast("子设备不能为空",false);
                                    return;
                                }
                            }
                        }
                        submit();
                    }
                });

                break;
        }
    }

    private void searchItemDevices(String mainWork) {
        if (mainWork.length() == 0) {
            childEquipmentAdapter.clear();
            llItemWorkLayout.setVisibility(View.GONE);
            return;
        }
        JsonObject map = new JsonObject();
        map.addProperty("actionType","Check");
        map.addProperty("mainEquipmentId",mainWork);
        map.addProperty("lotId",lotNum);

        NetUtils.requestDara("HIK.PdaGetSubEqp", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<ItemWorkBean>>
                (new TypeToken<BaseResponseBean<ItemWorkBean>>(){}.getType(),getSupportFragmentManager(),this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ItemWorkBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    ItemWorkBean body = response.body().getBody();
                    if (body.getSubEquipmentId().size() > 0){
                        llItemWorkLayout.setVisibility(View.VISIBLE);
                        childEquipmentAdapter.setNewData(body.getSubEquipmentId());
                    }else {
                        childEquipmentAdapter.clear();
                        llItemWorkLayout.setVisibility(View.GONE);
                    }

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ItemWorkBean>> response) {
                super.onError(response);
                llItemWorkLayout.setVisibility(View.GONE);
            }
        });
    }

    private void submit() {
        DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, new DialogUtils.signCallback() {
            @Override
            public void onSignSuccess() {
                JsonObject map = new JsonObject();
                map.addProperty("actionType", "Check");
                map.addProperty("lotId", lotNum);
                map.addProperty("mainEquipmentId", lbSpinner.getLabelContext().getTag().toString());
                map.addProperty("durableId", "");
                map.addProperty("subEquipmentId",childEquipmentAdapter.getItemWork());
                NetUtils.requestDara("HIK.FankePdaTrackIn", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>
                        (new TypeToken<BaseResponseBean<?>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<?>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            showConfirmDialog(Work_Al5TeosEtchTrackInActivity.this, "进站成功");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<?>> response) {
                        super.onError(response);
                        loadError(response.getException(), "HIK.FankePdaTrackIn");
                    }
                });
            }
        }, getSupportFragmentManager());
    }
}
